package com.engc.common.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.engc.common.bean.SysUserInfo;
import com.engc.common.dao.DBExecuteException;
import com.engc.common.service.CommonService;
import com.engc.common.service.impl.SystemService;
import com.engc.common.utils.Constant;

@Controller
@RequestMapping("/sysLogin")
public class SysLogin {

	@Resource
	private SystemService systemService;

	

	@RequestMapping("/doLogin")
	@ResponseBody
	public String doLogin(HttpServletRequest request, String username, String password, String validatecode) throws DBExecuteException {

		HttpSession session = request.getSession(true);
		// 验证验证码是否正确
		String validate_code = session.getAttribute(Constant.VALIDATE_CODE_SESS) == null ? "" : String.valueOf(session.getAttribute(Constant.VALIDATE_CODE_SESS));
		if (!validate_code.equals(validatecode)) {
			// 验证码错误
			return "-1";
		}
		// 验证账户密码是否正确
		SysUserInfo info = systemService.validateLogin(username, password);
		if (info == null) {
			// 用户名或者密码错误
			return "0";
		}
		
		session.setAttribute(Constant.SYS_USER_INFO, info);
		
		return "1";
	}
}
